package com.hae.shop.micoshopbasicssso.server.controller;

import com.hae.shop.common.base.Response;
import com.hae.shop.common.constant.ErrorEnum;
import com.hae.shop.member.model.reqDto.UserSSOLoginReqDto;
import com.hae.shop.member.model.respDto.UserLoginRespDto;
import com.hae.shop.micoshopbasicssso.feign.MemberFeign;
import com.hae.shop.micoshopbasicssso.server.core.result.ReturnT;
import com.xxl.sso.core.login.SsoTokenLoginHelper;
import com.xxl.sso.core.store.SsoLoginStore;
import com.xxl.sso.core.store.SsoSessionIdHelper;
import com.xxl.sso.core.user.XxlSsoUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

/**
 * sso com.hae.shop.micoshopbasicssso.server (for app)
 *
 * @author xuxueli 2018-04-08 21:02:54
 */
@Controller
@RequestMapping("/app")
public class AppController {

    @Autowired
    private MemberFeign memberFeign;


    /**
     * Login
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public ReturnT<String> login(String username, String password) {

        // valid login
        UserSSOLoginReqDto userSSOLoginReqDto = new UserSSOLoginReqDto();
        userSSOLoginReqDto.setMobile(username);
        userSSOLoginReqDto.setPassword(password);

        try {
            Response<UserLoginRespDto> loginResponse = memberFeign.loginBySSO(userSSOLoginReqDto);
            if (!loginResponse.isSuccess()) {
                return new ReturnT<String>(Integer.valueOf(loginResponse.getErrorCode()),
                        loginResponse.getErrorMsg());
            }

            // 1、make xxl-sso user
            XxlSsoUser xxlUser = new XxlSsoUser();
            xxlUser.setUserid(String.valueOf(loginResponse.getResult().getUserid()));
            xxlUser.setUsername(String.valueOf(loginResponse.getResult().getUserid()));
            xxlUser.setVersion(UUID.randomUUID().toString().replaceAll("-", ""));
            xxlUser.setExpireMinite(SsoLoginStore.getRedisExpireMinite());
            xxlUser.setExpireFreshTime(System.currentTimeMillis());


            // 2、generate sessionId + storeKey
            String sessionId = SsoSessionIdHelper.makeSessionId(xxlUser);

            // 3、login, store storeKey
            SsoTokenLoginHelper.login(sessionId, xxlUser);

            // 4、return sessionId
            return new ReturnT<String>(sessionId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnT<String>(Integer.valueOf(ErrorEnum.SYSTEM_INNER_EXCEPTION.getErrorCode()),
                    ErrorEnum.SYSTEM_INNER_EXCEPTION.getErrorMsg());
        }
    }


    /**
     * Logout
     *
     * @param sessionId
     * @return
     */
    @RequestMapping("/logout")
    @ResponseBody
    public ReturnT<String> logout(String sessionId) {
        // logout, remove storeKey
        SsoTokenLoginHelper.logout(sessionId);
        return ReturnT.SUCCESS;
    }

    /**
     * logincheck
     *
     * @param sessionId
     * @return
     */
    @RequestMapping("/logincheck")
    @ResponseBody
    public ReturnT<XxlSsoUser> logincheck(String sessionId) {

        // logout
        XxlSsoUser xxlUser = SsoTokenLoginHelper.loginCheck(sessionId);
        if (xxlUser == null) {
            return new ReturnT<XxlSsoUser>(ReturnT.FAIL_CODE, "sso not login.");
        }
        return new ReturnT<XxlSsoUser>(xxlUser);
    }

}